<?php defined('Restricted access'); ?>
<?php 
// http://ajax.googleapis.com/ajax/services/language/translate?v=1.0
//&q=hello%20world&q=goodbye&q=hello%20world&q=goodbye&q=hello%20world&q=goodbye&q=hello%20world&q=goodbye&q=hello%20world&q=goodbye
//&langpair=en|it
//echo "<pre>"; //var_dump($_SERVER['REQUEST_URI']); die;
//var_dump('sf', $this->translatorInfo); die;

// 解析URI 给AJAX地址
$rootDir = dirname($_SERVER['REQUEST_URI']);
$ajaxPagePath = "/components/com_joomlainitranslator/libraries/core/main.php";
$ajaxFullPath = $rootDir . $ajaxPagePath;
$jsonCode = json_encode($this->translatorInfo);
?>
<script src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
	google.load("jquery", "1.4.1");
	google.setOnLoadCallback(function(){
		jQuery.noConflict();
		jQuery(function($){
			// 处理数据
			var translatorInfo = <?php echo $jsonCode;?>;
			translatorInfo = translatorInfo[0];

			// 文件翻译请求
			var dataSenderTranslate = function(filename) {
				$.ajax({
					type:	"POST",
					url:	"<?php echo $ajaxFullPath; ?>",
					data:	"filename=" + filename,
					async:	false,
					success: function(msg){
						// 返回成功标识 并且返回所有已经翻译完成的内容
						if (msg == 'success') {
							addTranslatorDetail("file:" + filename + " translate success!");
						}
					}
				});
			}
			
			// 进度条
			var processBar = function(per, filename) {
				$("#processBar").animate({width:per + "%"}, 1000, function(){
					// 发送翻译请求
					if (per < 100) {
						$("#processBar span").html(per + "%  |  当前处理文件: " + filename);
						dataSenderTranslate(filename);
					} else {
						$("#processBar span").html(per + "%  |  文件处理完成");
					}
				});
			}

			// 详细追加
			var addTranslatorDetail = function(info) {
				$("#processDetailBox textarea").html($("#processDetailBox textarea").html() + info + "\n");
			}
			
			// 处理请求返回结果
			var parseList = function(list) {
				for (i = 0; i <= list.length; i++) {
					// 进度条
					var per = (i / list.length) * 100;
					processBar(parseInt(per), list[i]);
				}
			}
			
			// 发送请求初始化
			var dataSenderInit = function(ti){
				$.ajax({
					type:	"POST",
					url:	"<?php echo $ajaxFullPath;?>",
					data:	"srcLangCode=" + ti.srcLangCode + "&srcLangDir=" + ti.srcLangDir + "&tarLangCode=" + ti.tarLangCode + "&tarLangDir=" + ti.tarLangDir + "&langDir=" + ti.langDir,
					success: function(list) {
						addTranslatorDetail("init finish");
						addTranslatorDetail("session clear");
						parseList(eval(list));
					}
				});
			}
			dataSenderInit(translatorInfo);
		});
	});
</script>
<style type="text/css">
#processBar {
	width:1px;
	height:30px;
	background-color:#FF8604;
}
#processBar span {
	position:absolute;
	width:100%;
	text-align:center;
	height: 100%;
	line-height:30px;
}
#processDetailBox textarea {
	width:100%;
	height:300px;
}
</style>
<form action="index.php" method="post" name="adminForm" id="adminForm">
<div class="col100">
	<fieldset class="processStatusBar">
		<legend><?php echo JText::_( 'Processing Percent' ); ?></legend>
		<div id="processBar"><span id="pb"><?php echo JText::_('Process Bar Ready')?></span></div>
	</fieldset>
	<fieldset class="processDetailInfo">
		<legend><?php echo JText::_( 'Translator Processing' ); ?></legend>
		<div id="processDetailBox">
			<textarea></textarea>
		</div>
	</fieldset>
</div>
<div class="clr"></div>
<input type="hidden" name="option" value="com_joomlainitranslator" />
<input type="hidden" name="task" value="" />
<input type="hidden" name="controller" value="translator" />
<input type="hidden" name="status" value="never" />
</form>